﻿using AutoMapper;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.AspNetCore.Mvc;
using RxxAdmin.Common.Helper;
using RxxAdmin.Common.HttpContextUser;
using RxxAdmin.IServices;
using RxxAdmin.Model;
using RxxAdmin.Model.Entity;
using RxxAdmin.Model.ViewModels;
using RxxAdmin.Services;
using StackExchange.Redis;
using System.Data;
using System.DirectoryServices.Protocols;
using System.Security.Permissions;

namespace RxxAdmin.Api.Controllers.permission;
/// <summary>
/// 角色管理
/// </summary>
public class SysRoleController : BaseApiController
{
    private readonly ISysRoleService _sysRoleService;
    private readonly ISysRoleMenuService _sysRoleMenuService;
    private readonly IMapper _mapper;

    public SysRoleController(ISysRoleService sysRoleService, ISysRoleMenuService sysRoleMenuService, IMapper mapper)
    {
        _sysRoleService = sysRoleService;
        _sysRoleMenuService = sysRoleMenuService;
        _mapper = mapper;
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <returns></returns> 
    [HttpGet]
    public async Task<MessageModel<PageModel<SysRole>>> PageList([FromQuery] SysRolePageIn input)
    {
        if (string.IsNullOrEmpty(input.RoleName) || string.IsNullOrWhiteSpace(input.RoleName))
        {
            input.RoleName = "";
        }
        var users = await _sysRoleService.QueryPage(
                    a => a.Status >= 0 && a.Name != null && a.Name.Contains(input.RoleName),
                    input.PageNum,
                    input.PageSize,
                    " Id desc ");
        return Success(users);
    }
    [HttpGet]
    public async Task<MessageModel<List<SysRole>>> List()
    {
        return Success((await _sysRoleService.Query()).Where(x => x.IsSeed == 1).ToList());
    }
    /// <summary>
    /// 添加角色
    /// </summary>
    /// <returns></returns> 
    [HttpPost]
    public async Task<MessageModel<string>> Add(SysRole input)
    {
        input.Id = 5;
        var id = await _sysRoleService.Add(input);
        return id > 0 ? Success(id.ObjToString(), "添加成功") : Failed("添加失败");
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <returns></returns> 
    [HttpDelete("{id}")]
    public async Task<MessageModel<string>> Delete(int id)
    {
        if (id <= 0) return Failed();
        var userDetail = await _sysRoleService.QueryById(id);
        if (userDetail == null) return Success<string>(null, "角色不存在");
        var roleMenus = (await _sysRoleMenuService.Query()).Where(x => x.RoleId == id).ToList();
        var bo = await _sysRoleMenuService.DeleteByIds(Array.ConvertAll(roleMenus.Select(x => x.Id).ToArray(), Convert.ToString));
        return await _sysRoleService.Delete(userDetail) ? Success(userDetail?.Id.ObjToString(), "删除成功") : Failed();
    }

    /// <summary>
    /// 更新角色
    /// </summary>
    /// <returns></returns> 
    [HttpPut]
    public async Task<MessageModel<string>> Update(SysRole input)
    {
        if (input == null || input.Id <= 0)
            return Failed("缺少参数");

        return await _sysRoleService.Update(input) ? Success(input?.Id.ObjToString(), "更新成功") : Failed("更新失败");
    }
}
